package com.lkd.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class CompeleteHandler implements MsgHandler {
    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        String innerCode = taskCompleteContract.getInnerCode();
        int taskType = taskCompleteContract.getTaskType();
        VendingMachineEntity vmInfo = vendingMachineService.getOne(new LambdaQueryWrapper<VendingMachineEntity>().eq(VendingMachineEntity::getInnerCode, innerCode));
        if(null == vmInfo){
            throw new LogicException("请核验设备编号");
        }
        if(taskType==VMSystem.TASK_TYPE_DEPLOY) {
            vmInfo.setVmStatus(VMSystem.VM_STATUS_RUNNING);
        }else if(taskType==VMSystem.TASK_TYPE_REVOKE){
            vmInfo.setVmStatus(VMSystem.VM_STATUS_REVOKE);
        }
        vendingMachineService.updateById(vmInfo);
    }
}
